#!/usr/bin/php
<?php
require_once '/data/wwwroot/g_vendor/autoload.php';

use splitbrain\phpcli\CLI;
use splitbrain\phpcli\Options;

class Minimal extends CLI
{
    const DEFAULT_PATH_CONTROLLER = "/application/controllers/";
    const DEFAULT_PATH_API = "/application/actions/";

    /**
     * Register options and arguments on the given $options object
     *
     * @param Options $options
     * @return void
     */
    protected function setup(Options $options)
    {
        $options->setHelp('This example sets up additional subcommands using their own options');
        $options->registerOption('longflag', 'This is a global flag that applies to all subcommands', 'l');

        $options->registerCommand('create:controller', 'Create a new controller class');
        $options->registerCommand('create:api', 'Create a new api class');
        //$options->registerCommand('bar', 'The bar command');
        //$options->registerOption('someflag', '这是对create命令的一个解释', 's', false, 'create');
        self::supportAllCreate($options, "create:controller", false);
        self::supportAllCreate($options, "create:api", true);

    }

    public static function supportAllCreate(Options $options, $command, $is_fold = true)
    {
        if ($is_fold == true) {
            $options->registerArgument('fold', 'Fill in the name of the folder', false, $command);
            $options->registerArgument('file', 'Fill in the file name', true, $command);
        } else {
            $options->registerArgument('file', 'Fill in the file name', true, $command);
        }

    }

    private static function createFoldAndFile($command_name, $foldName, $fileName)
    {
        if ($command_name == "create:controller") {
            $default_path = dirname(__FILE__) . self::DEFAULT_PATH_CONTROLLER;
        } elseif ($command_name == "create:api") {
            $default_path = dirname(__FILE__) . self::DEFAULT_PATH_API;
        }
        if ($foldName) {
            $path = $default_path . $foldName . "/" . $fileName;
            $fold_path = $default_path . $foldName;
            //此处可以使用变量名组成的字符串来动态创建文件
            if (!file_exists($fold_path)) {//检查文件夹是否存在
                mkdir($fold_path, '0777');    //没有就创建一个新文件夹
            }
        } else {
            $path = $default_path . $fileName;
            //此处可以使用变量名组成的字符串来动态创建文件
            if (!file_exists($default_path)) {//检查文件夹是否存在
                mkdir($default_path, '0777');    //没有就创建一个新文件夹
            }
        }
        if (!file_exists($path)) {
            $file_info = explode(".", $fileName);
            $prefix_name = $file_info[0];
            $str = self::getFileStr($command_name, $prefix_name);
            //join函数返回一个被指定分隔符分割后字符串
            file_put_contents($path, $str);
            echo "{$fileName}文件创建成功!!";
        }
    }

    //获取不同命令下创建的文件自带的默认信息
    public static function getFileStr($command_name, $prefix_name)
    {
        if ($command_name == "create:controller") {
            $str1 = "
<?php
class " . $prefix_name . "Controller extends Yaf_Controller_Abstract
";
            $str2 = '
{
    public $actions = [
    ];
}
';
            $str = $str1 . $str2;
        } elseif ($command_name == "create:api") {
            $str = "
<?php


class " . $prefix_name . "Action extends ActionPlugin
{

    public function main()
    {
       
    }
}";
        }
        return $str;
    }

    /**
     * Your main program
     *
     * Arguments and options have been parsed when this is run
     *
     * @param Options $options
     * @return void
     */
    protected function main(Options $options)
    {
        $command_name = $options->getCmd();
        switch ($options->getCmd()) {
            case 'create:controller':
                $this->success('The create:controller command was called');
                //处理create:controller命令
                $this->info('$options->getArgs():');
                //获取fileInfo信息
                $fileInfo = $options->getArgs();
                //var_dump($fileInfo);
                $fileName = $fileInfo[0];
                //创建文件夹和文件
                self::createFoldAndFile($command_name, "", $fileName);
                break;
            case 'create:api':
                $this->success('The create:api command was called');
                //处理create:controller命令
                $this->info('$options->getArgs():');
                //获取fileInfo信息
                $fileInfo = $options->getArgs();
                //var_dump($fileInfo);
                $foldName = $fileInfo[0];
                $fileName = $fileInfo[1];
                //创建文件夹和文件
                self::createFoldAndFile($command_name, $foldName, $fileName);
                break;
            case 'compact':
                $options->useCompactHelp();
                echo $options->help();
                exit;
            default:
                $this->error('No known command was called, we show the default help instead:');
                echo $options->help();
                exit;
        }

    }
}

$cli = new Minimal();
$cli->run();